// pages/component/homes/purchase/rechargepay/rechargepay.js
var that;
var dataManager = require('../../../../../utils/dataManager.js');
var GlobalData = require('../../../../../utils/GlobalData.js');
var Util = require('../../../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopcarArr: GlobalData.shopcar_2,
    price: 0,
    playerID: 0,
    playername: "",
    choose: false,
    hidden: true,
    // isshow:false,
    item: {
      title: "",
      playerName: "",
      playerId: "",
    },
    isTemplateshow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dataManager.removeBtnObserver("rechargepay")
    // console.log(GlobalData.shopcar_2, "房卡");
    this.setData({
      hidden: true
    })
    let pri = 0;
    for (let i = 0; i < GlobalData.shopcar_2.length; i++) {
      pri += GlobalData.shopcar_2[i].pro.MemberPrice * GlobalData.shopcar_2[i].count;
    }
    pri = Math.round(pri * 100) / 100;
    this.setData({
      price: pri,
      shopcarArr: GlobalData.shopcar_2
    })

    // console.log(this.data.shopcarArr);
  },
  /**获得玩家id */
  getPlayId: function (e) {
    // console.log(e.detail.value);
    var that = this;
    that.setData({
      playerID: e.detail.value
    });
    if (!that.data.playerID) {
      // app.show("请输入正确的游戏ID。");
      return;
    }
    if (e.detail.value) {
      GlobalData.rechargeId = that.data.playerID;
      dataManager.rigisterObserver("playername", that.onNameComplete, that);
      dataManager.getPlayerName(that.onInfoError);
    }

  },
  onInfoError: function () {
    this.setData({
      playername: "",
      choose: false
    })

    GlobalData.PlayerName = ""
  },
  onNameComplete: function () {
    var that = this;
    that.setData({
      playername: GlobalData.PlayerName,
      choose: true
    })

    if (!that.data.playername) {
      that.setData({
        choose: false
      })
      app.show("不存在此玩家。");
      return;
    }
    if (that.data.playername)
      {
        that.setData({
          isTemplateshow:true,
          item: {
            title: "游戏直充",
            playerName: that.data.playername,
            playerId: that.data.playerID,
          }
        })
      }
      // wx.showModal({
      //   title:"游戏直充",
      //   content: "是否为该玩家充值？" + that.data.playername + "(id:" + that.data.playerID + ")",
      //   // showCancel: true,
      //   cancelText: "返回",
      //   confirmText: "确认充值",
      //   success: function (res) {
      //     //全部取出
      //     if (res.confirm) {
      //       that.onPayClick();
      //     } else {
      //       // 存放卡库
      //     }

      //   }
      // })
  },
  //点击弹框返回按钮
  onTurnoff: function () {
    console.log("1")
    this.setData({
      isTemplateshow:false
    })
  },
  //点击弹框确认充值按钮
  onTurnon:function(){
    this.onPayClick();
  },
  // 点击确认购买按钮
  onOk: function () {
    var that = this
    if (!that.data.playerID) {
      app.show("请输入正确的玩家ID。");
      return;
    }
    if (that.data.playername) {
      that.setData({
        isTemplateshow: true,
        item: {
          title: "游戏直充",
          playerName: that.data.playername,
          playerId: that.data.playerID,
        }
      })
      // wx.showModal({
      //   content: "是否为该玩家充值？" + that.data.playername + "(id:" + that.data.playerID + ")",
      //   // showCancel: true,
      //   cancelText: "返回",
      //   confirmText: "确认充值",
      //   success: function (res) {
      //     //全部取出
      //     if (res.confirm) {
      //       that.onPayClick();
      //     } else {
      //       // 存放卡库
      //     }

      //   }
      // })
    } else {
      app.show("请输入正确的玩家ID。");
    }


  },
  onPayClick: function () {
    var that = this;
    let arr = [];
    for (let i = 0; i < GlobalData.shopcar_2.length; i++) {
      let obj = {};
      obj["sysno"] = GlobalData.shopcar_2[i].pro.SysNo;
      obj["count"] = GlobalData.shopcar_2[i].count;
      arr.push(obj);
    }
    let str = JSON.stringify(arr);
    this.setData({
      hidden: false
    })
     let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "addorder", token: GlobalData.token, "ordertype": 1, "paytype": 0, "products": str, "uid": this.data.playerID, source: "SMAPP" }),
      complete: function (res) {
        let pkg = res.data;
        console.log(pkg, "在数据管理器中获取主页信息");
        if (pkg && pkg.errcode == 0) {
          //清空购物车
          // GlobalData.shopcar_2.splice(0, GlobalData.shopcar_2.length);
          //清空直充记录
          // GlobalData.RechargeRecord = null;
          GlobalData.order = {};
          GlobalData.order["orderno"] = pkg.data.orderno;
          GlobalData.order["money"] = pkg.data.totalprice;
          GlobalData.order["sysno"] = pkg.data.sysno;
          GlobalData.order["rechargetype"] = "游戏直充";
          GlobalData.order["nametype"] = "玩家"
          GlobalData.order["playername"] = pkg.data.nickname;
          GlobalData.order["idtype"] = "玩家"
          GlobalData.order["playerid"] = pkg.data.uid;
          GlobalData.order["owntype"] = true;
          if (!GlobalData.order["time"])
            GlobalData.order["time"] = that.getDate();
          GlobalData.PayReason = 3;
          // wx.navigateTo({
          //   url: '../../purchase/payment/payment'
          // })
          dataManager.OnRequestPost({ method: "getpaytype", "type": 3 }, that.onProcessgetpaytype, false, that.onError);

        } else {
          that.setData({
            hidden: true
          })
          app.onTip(pkg.errmsg)
        }
      }
    })

  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  /**获取支付类型 */
  onProcessgetpaytype: function (data) {
    let arr = [];
    for (let i = 0; i < data.length; i++) {
      arr[i] = {};
      arr[i] = GlobalData.clone(data[i]);
    }
    GlobalData.PayType[GlobalData.PayReason] = arr;

    // if (dataManager.isClick("payment")) {
    //   return
    // } else {
    //   dataManager.btnObserver("payment")
    this.setData({
      hidden: true
    })
    wx.redirectTo({
      url: '../../purchase/payment/payment',
    })
    // }


  },
  getDate: function () {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var date = d.getDate();
    var day = d.getDay();
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();
    var ms = d.getMilliseconds();
    var curDateTime = year;
    if (month > 9)
      curDateTime = curDateTime + "-" + month;
    else
      curDateTime = curDateTime + "-0" + month;
    if (date > 9)
      curDateTime = curDateTime + "-" + date;
    else
      curDateTime = curDateTime + "-0" + date;
    curDateTime = curDateTime + " ";
    if (hours > 9)
      curDateTime = curDateTime + "" + hours;
    else
      curDateTime = curDateTime + "0" + hours;
    if (minutes > 9)
      curDateTime = curDateTime + ":" + minutes;
    else
      curDateTime = curDateTime + ":0" + minutes;
    if (seconds > 9)
      curDateTime = curDateTime + ":" + seconds;
    else
      curDateTime = curDateTime + ":0" + seconds;
    return curDateTime;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})